package com.wisemoresystem.seal.action.useapply;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_role;
import com.wisemoresystem.seal.model.t_seal_use_apply;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.MacService;
import com.wisemoresystem.seal.service.UseApplyService;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.service.WordService;
import com.wisemoresystem.seal.util.DateTools;
import com.wisemoresystem.seal.util.json.JsonTools;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 用印申请
 * 
 * @author Administrator
 *
 */
@Controller("ixSealUseApply")
@Scope("prototype")
@RequestMapping("/ix/useapply/apply")
public class SealUseApplyAction extends ProjBaseAction {

	@Autowired
	private UseApplyService uAService;
	@Autowired
	private MacService mService;
	@Autowired
	private UserService userService;
	@Autowired
	private WordService wService;

	/**
	 * 使用申请列表
	 * 
	 * @param srh_reason
	 *            用印事由
	 * @param srh_user
	 *            申请人
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_reason, String srh_user) {

		List<Map<String, Object>> list = uAService.selectUseApplyList(
				srh_reason, srh_user, page);

		ModelAndView mv = new ModelAndView("/jsp/useapply/apply/list");
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/add_form")
	public ModelAndView add_form() {

		List<Map<String, Object>> macList = mService.selectMac(WebTools
				.getCurrentBankId());
		List<Map<String, Object>> sealList = uAService.selectSeal(WebTools
				.getCurrentBankId());
		t_role role = oc.findObjById(WebTools.getCurrentUser().getRole_id(),
				t_role.class);

		ModelAndView mv = new ModelAndView("/jsp/useapply/apply/add_form");
		mv.addObject("macList", macList);
		mv.addObject("sealList", sealList);
		mv.addObject("roleName", role.getName());
		mv.addObject("applyTime", DateTools.getTimestampTodayStr());
		return mv;
	}

	@RequestMapping("/add")
	public ModelAndView add(t_seal_use_apply apply) {

		apply.setBank_id(WebTools.getCurrentBankId());
		apply.setPass_status("草稿");
		oc.saveObj(apply);

		super.setTipMsg("操作成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	@RequestMapping("/edit_form")
	public ModelAndView edit_form(String apply_id) {

		List<Map<String, Object>> macList = mService.selectMac(WebTools
				.getCurrentBankId());
		List<Map<String, Object>> sealList = uAService.selectSeal(WebTools
				.getCurrentBankId());
		t_seal_use_apply apply = oc.findObjById(apply_id,
				t_seal_use_apply.class);
		t_user user = oc.findObjById(apply.getApply_user(), t_user.class);
		Map<String, Object> files = uAService.getMapFiles(apply);

		ModelAndView mv = new ModelAndView("/jsp/useapply/apply/edit_form");
		mv.addObject("macList", macList);
		mv.addObject("sealList", sealList);
		mv.addObject("apply", apply);
		mv.addObject("user", user);
		mv.addObject("files", files);
		return mv;
	}

	@RequestMapping("/edit")
	public ModelAndView edit(t_seal_use_apply apply, String apply_id) {

		t_seal_use_apply applyDb = oc.findObjById(apply_id,
				t_seal_use_apply.class);
		applyDb.setReason(apply.getReason());
		applyDb.setCompany(apply.getCompany());
		applyDb.setSeal_id(apply.getSeal_id());
		applyDb.setMac_id(apply.getMac_id());
		applyDb.setIs_out(apply.getIs_out());
		applyDb.setUse_type(apply.getUse_type());
		applyDb.setCopes(apply.getCopes());
		applyDb.setNum(apply.getNum());
		applyDb.setApply_user(apply.getApply_user());
		applyDb.setApply_department(apply.getApply_department());
		applyDb.setPromoter(apply.getPromoter());
		applyDb.setApply_time(apply.getApply_time());
		applyDb.setNote(apply.getNote());
		applyDb.setType_name(apply.getType_name());
		applyDb.setWord_name(apply.getWord_name());
		applyDb.setOther_note(apply.getOther_note());
		applyDb.setApply_file_ids(apply.getApply_file_ids());
		applyDb.setCheck_file_ids(apply.getCheck_file_ids());
		applyDb.setLaw_file_ids(apply.getLaw_file_ids());
		applyDb.setSelf_file_ids(apply.getSelf_file_ids());
		applyDb.setOther_file_ids(apply.getOther_file_ids());
		oc.updateObj(applyDb);

		super.setTipMsg("操作成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	@RequestMapping("/del")
	public ModelAndView del(String apply_id) {

		oc.delObjById(apply_id, t_seal_use_apply.class);

		super.setTipMsg("操作成功！", Tip.Type.success);
		ModelAndView mv = new ModelAndView("redirect:list.htm");
		return mv;
	}

	@RequestMapping("/get_user")
	public ModelAndView get_user() {

		List<Map<String, Object>> userList = userService.selectUserLists(
				WebTools.getCurrentBankId(), page);

		ModelAndView mv = new ModelAndView("/jsp/useapply/get/get_user");
		mv.addObject("userList", userList);
		return mv;
	}

	@RequestMapping("/get_type")
	public ModelAndView get_type() {

		List<Map<String, Object>> list = wService.selectTypeWordList();

		ModelAndView mv = new ModelAndView("/jsp/useapply/get/get_type");
		mv.addObject("list", list);
		return mv;
	}

	@RequestMapping("/get_word")
	public ModelAndView get_word() {

		List<Map<String, Object>> list = wService.selectWordTypeList();

		ModelAndView mv = new ModelAndView("/jsp/useapply/get/get_word");
		mv.addObject("list", list);
		return mv;
	}

	/**
	 * 需要盖章的apply list json
	 * 
	 * @return
	 */
	@RequestMapping("/list_json")
	@ResponseBody
	public String list() {

		List<Map<String, Object>> applyList = uAService
				.selectUseApplyList(null,null,page);

		return JsonTools.toJson(applyList);
	}
}
